//Header Info This is a sample main.c template with interrupt handling to start as a jump point for new projects. We will always want to list the version of the IDE and the version of the C18 we are compiling our data with. The reason we do this is for bug checking and tracking as code updates later on can effect the code we are compiling. This can make troubleshooting easier for others who maybe compling on a different plateform version than us. /* main.c MPLabs IDE version x.xx MPLabs C18 version x.xx */ // Included Header Files This area is where we place all the header files that the project will be using. You will always need to inlcude the proper header file for the version of the pic18f you are using such as if you are using a PIC18F1440 type pic. {EDIT: To match your pic type} #include // configure pic based on available options This area is used to setup predefined pic configuration settings we want to enable by default in our project. These optional values vary based on the pic and its supported features. Most pics will have by default options such as WatchDog Timers, PORTB Analog Enabled, and Oscillator types such as Internal or External and what type of external oscillator you will be using. Refer to the documentation for your selected PIC type. {EDIT: Refer to PIC18_config.pdf for your pic type} #pragma config OSC=HSPLL, WDT=OFF, PWRT=OFF, MCLRE=OFF, LVP=OFF, DEBUG=ON, PBADEN = OFF // Declarations for project This area is designated for defining set values such as the FOSC value for the timer functions and also declare function names that occure later on in the project such as main, configuration, interrupt routines, and so on. You can also place information such as definition for pin names such as #define Input0 PORTAbits.RA0 as an example. #define FOSC 32000000 // defined based on a 8.00 mhz crystal clocking with pll = 32.00mhz void main (void); void configuration (void); #pragma code high_vector=0x08 void interrupt_at_high_vector(void){ _asm goto hi_isr _endasm} #pragma code low_vector=0x18 void interrupt_at_low_vector(void) { _asm goto lo_isr _endasm} // configuration routine to setup PORT(x) pin This section of code is used to define the pin functions on startup. The pins are by default reset to their power on state as defined by the documentation for your pic of choice. Refer to each PORTx section of the documenation to determin the default pin state and if any changes need to be made to configure the pin for our particular use. The following code is a C18 example as defined in the documentation for configuring PORTA to digital inputs. void configuration (void) { // Lets Configure PORTA for digital input // Initialize by clearing output data latches LATA = 0x00; // set pins from A/D to digital usage ADCON1 = 0x0F; // set comparators for digital usage CMCON = 0x07; // set as digital inputs TRISA = 0xCF; // Continue configuring PORT-B to PORT-E {Place your additional code here} // Lets Configure Interrupt Handling RCONbits.IPEN = 1; // Enable interrupt priority levels INTCONbits.GIEH = 1; // Enable all high priority interrupts } // main routine to handle the loop processing code This main routine is the heart of the code. This will run an endless loop cycle calling other routines we may have defined in our project. If this project is configured to use interrupts, when an interrupt occures the main loop will stop execution and jump to the defined interrupt handler to process the interrupt that occured, once this is handled execution is handed back to the main routine. void main (void) { // Initialize the Microcontroller configuration(); while (1) { {Your main code loop runs here} } } // end main // High ISR Routine #pragma interrupt hi_isr void hi_isr(void) { {Your High interrupt service code goes here} return; } // Low ISR Routine #pragma interruptlow lo_isr void lo_isr(void) { {Your Low interrupt service code goes here} return; }